package com.gf.snoopy.util;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.gf.snoopy.domain.Buyer;
import com.gf.snoopy.domain.Project;
import com.gf.snoopy.domain.SubProject;
import com.gf.snoopy.constants.enumc.*;
import com.gf.snoopy.platform.exception.PokeException;

public class ParseUtil {
	
	private static final Logger logger = Logger.getLogger(ParseUtil.class);

	public static Map<String, Object> parseProject(Project project)
			throws PokeException {
		String name = project.getName();
		String short_name = project.getShortName();
		String manager = project.getManager();
		Date expireDate = project.getExpiredate();
		Date beginDate = project.getBegindate();
		Date establishDate = project.getEstablishdate();
		Date noobjectionDate = project.getNoobjectiondate();
		String noobjectionCode = project.getNoobjectionCode();
		String depositUrl = project.getDepositUrl();
		Date confirmDate = project.getConfirmdate();
		String issueprice = String.valueOf(project.getIssueprice());
		String threshold = String.valueOf(project.getThreshold());
		int additional = project.getAdditional();
		Integer unit = project.getUnit();
		String deposit = project.getDepositId();
		String regist = project.getRegist().getName();
		String exchange = project.getExchange().getName();
		String rating = project.getRating().getName();

		Map<String, Object> dataMap = new HashMap<String, Object>();
		dataMap.put("name", name);
		dataMap.put("short_name", short_name);
		dataMap.put("manager", manager);
		dataMap.put("expireDate", expireDate);
		dataMap.put("beginDate", beginDate);
		dataMap.put("establishDate", establishDate);
		dataMap.put("noobjectionDate_nyr", datetoNYR(noobjectionDate));
		dataMap.put("noobjectionCode", noobjectionCode);
		dataMap.put("depositUrl", depositUrl);
		dataMap.put("confirmDate", confirmDate);
		dataMap.put("confirmDate_nyr", datetoNYR(confirmDate));
		dataMap.put("issueprice", issueprice);
		dataMap.put("threshold", threshold);
		dataMap.put("threshold_shou", toShou(String.valueOf(threshold)));
		dataMap.put("additional", additional);
		dataMap.put("additional_shou", toShou(String.valueOf(additional)));
		dataMap.put("unit", unit);
		dataMap.put("deposit", deposit);
		dataMap.put("regist", regist);
		dataMap.put("exchange", exchange);
		dataMap.put("rating_company", rating);
		dataMap.put("expireDate_year", getYear(expireDate));
		dataMap.put("expireDate_month", getMonth(expireDate));
		dataMap.put("expireDate_day", getDay(expireDate));
		dataMap.put("confirmDate_year", getYear(confirmDate));
		dataMap.put("confirmDate_month", getMonth(confirmDate));
		dataMap.put("confirmDate_day", getDay(confirmDate));

		// 子项目信息
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		List<Map<String, Object>> list2 = new ArrayList<Map<String, Object>>();
		List<SubProject> subprojects = project.getSubprojects();

		int num = subprojects.size();
		// 文档日期
		Calendar c = Calendar.getInstance();
		c.setTime(confirmDate);
		c.add(Calendar.DATE, 1);
		if (c.get(Calendar.DAY_OF_WEEK) == 7) {
			c.add(Calendar.DATE, 2);
		}
		if (c.get(Calendar.DAY_OF_WEEK) == 1) {
			c.add(Calendar.DATE, 1);
		}
		int year = c.get(Calendar.YEAR);
		int month = c.get(Calendar.MONTH) + 1;
		int day = c.get(Calendar.DAY_OF_MONTH);
		String sysdate = year + "年" + month + "月" + day + "日";
		dataMap.put("sysDate", sysdate);

		int index = 1;
		dataMap.put("num", num);
		//
		for (SubProject s : subprojects) {
			Map<String, Object> map = new HashMap<String, Object>();

			// ///////////////////////
			map.put("sq", String.valueOf(index));

			map.put("name", name);
			map.put("short_name", short_name);
			map.put("manager", manager);
			map.put("expireDate", expireDate);
			map.put("beginDate", beginDate);
			map.put("establishDate", establishDate);
			map.put("noobjectionDate_nyr", datetoNYR(noobjectionDate));
			map.put("noobjectionCode", noobjectionCode);
			map.put("depositUrl", depositUrl);
			map.put("confirmDate", confirmDate);
			map.put("confirmDate_nyr", datetoNYR(confirmDate));
			map.put("issueprice", issueprice);
			map.put("threshold", threshold);
			map.put("threshold_shou", toShou(String.valueOf(threshold)));
			map.put("additional", additional);
			map.put("additional_shou", toShou(String.valueOf(additional)));
			map.put("unit", unit);
			map.put("deposit", deposit);
			map.put("regist", regist);
			map.put("exchange", exchange);
			map.put("rating_company", rating);
			map.put("sysdate", sysdate);
			map.put("expireDate_year", getYear(expireDate));
			map.put("expireDate_month", getMonth(expireDate));
			map.put("expireDate_day", getDay(expireDate));
			map.put("confirmDate_year", getYear(confirmDate));
			map.put("confirmDate_month", getMonth(confirmDate));
			map.put("confirmDate_day", getDay(confirmDate));

			String subproject_name = s.getName();
			String subproject_short_name = s.getShortName();
			String subproject_code = s.getCode();
			String subproject_collect = s.getCollect();
			Date subproject_interestDate = s.getInterestdate();
			Date subproject_expiryDate = s.getExpirydate();
			String subproject_rate = s.getRate().toString();
			String subproject_interestType = InterestType.parse(
					s.getInteresttype()).getDesc();
			String subproject_payType = PayType.parse(s.getPaytype()).getDesc();
			String subproject_payNumber = s.getPaynumber().toString();
			String subproject_credit = s.getCredit();
			String subproject_rating = s.getRating().getName();
			String subproject_memo = s.getMemo();
			String subproject_isTrade = IsTrade.parse(s.getIsTrade()).getDesc();
			Date subproject_ratingDate = s.getRatingDate();
			String duration = s.getDuration();
			String payMonth = s.getPayMonth();
			String payDay = s.getPayDay();
			String subproject_payDate = payDate(subproject_interestDate,
					subproject_expiryDate, payMonth, payDay, s.getPaytype());
			String subproject_duration_year = String.valueOf(duration_year(
					subproject_interestDate, subproject_expiryDate));
			String subproject_duration_day = String.valueOf(duration_day(
					subproject_interestDate, subproject_expiryDate));

			map.put("subproject_name", subproject_name);
			map.put("subproject_short_name", subproject_short_name);
			map.put("subproject_code", subproject_code);
			map.put("subproject_collect", subproject_collect);
			map.put("subproject_collect_yiyuan", toYiyuan(subproject_collect));
			map.put("subproject_collect_fen", toFen(subproject_collect));
			map.put("subproject_collect_wanyuan", toWanyuan(subproject_collect));
			map.put("subproject_interestDate", subproject_interestDate);
			map.put("subproject_interestDate_nyr",
					datetoNYR(subproject_interestDate));
			map.put("subproject_expiryDate", subproject_expiryDate);
			map.put("subproject_expiryDate_nyr",
					datetoNYR(subproject_expiryDate));
			map.put("subproject_rate", subproject_rate);
			map.put("subproject_interestType", subproject_interestType);
			map.put("subproject_payType", subproject_payType);
			map.put("subproject_payNumber", subproject_payNumber);
			map.put("subproject_credit", subproject_credit);
			map.put("subproject_rating", subproject_rating);
			map.put("subproject_memo", subproject_memo);
			map.put("subproject_isTrade", subproject_isTrade);
			map.put("subproject_ratingDate", subproject_ratingDate);
			map.put("subproject_ratingDate_nyr",
					datetoNYR(subproject_ratingDate));
			map.put("duration", duration);
			map.put("payMonth", payMonth);
			map.put("payDay", payDay);
			map.put("subproject_payDate", subproject_payDate);
			map.put("subproject_duration_day", subproject_duration_day);
			map.put("subproject_duration_year", subproject_duration_year);

			List<Buyer> buyers = s.getBuyers();

			for (Buyer b : buyers) {
				Map<String, Object> map2 = new HashMap<String, Object>();
				String buyer_name = b.getName();
				String buyer_code = b.getCode();
				Integer buyer_number = b.getNumber();
				String buyer_id = b.getBuyerId();
				String buyer_tradeunit = b.getTradeUnit();

				map2.put("subproject_code", subproject_code);
				map2.put("buyer_name", buyer_name);
				map2.put("buyer_code", buyer_code);
				map2.put("buyer_number", buyer_number);
				map2.put("buyer_id", buyer_id);
				map2.put("buyer_tradeunit", buyer_tradeunit);
				map2.put("sysdate", sysdate);
				list2.add(map2);

			}

			list.add(map);

			index++;
		}

		dataMap.put("subproject", list);
		dataMap.put("buyer", list2);

		return dataMap;
	}

	public static Map<String, Object> parseSubProject(SubProject s)
			throws PokeException {

		List<Map<String, Object>> list2 = new ArrayList<Map<String, Object>>();
		Map<String, Object> map = new HashMap<String, Object>();
		Project p = s.getProject();
		// 文档日期
		Calendar c = Calendar.getInstance();
		c.setTime(p.getConfirmdate());
		c.add(Calendar.DATE, 2);
		if (c.get(Calendar.DAY_OF_WEEK) == 7) {
			c.add(Calendar.DATE, 2);
		}
		if (c.get(Calendar.DAY_OF_WEEK) == 1) {
			c.add(Calendar.DATE, 1);
		}
		int year = c.get(Calendar.YEAR);
		int month = c.get(Calendar.MONTH) + 1;
		int day = c.get(Calendar.DAY_OF_MONTH);
		String sysdate = year + "年" + month + "月" + day + "日";
		map.put("sysDate", sysdate);
		// 序号
		int index = 1;
		String subproject_name = s.getName();
		String subproject_short_name = s.getShortName();
		String subproject_code = s.getCode();
		String subproject_collect = s.getCollect();
		Date subproject_interestDate = s.getInterestdate();
		Date subproject_expiryDate = s.getExpirydate();
		String subproject_rate = s.getRate().toString();
		String subproject_interestType = InterestType
				.parse(s.getInteresttype()).getDesc();
		String subproject_payType = PayType.parse(s.getPaytype()).getDesc();
		String subproject_payNumber = s.getPaynumber().toString();
		String subproject_credit = s.getCredit();
		String subproject_rating = s.getRating().getName();
		String subproject_memo = s.getMemo();
		String subproject_isTrade = IsTrade.parse(s.getIsTrade()).getDesc();
		Date subproject_ratingDate = s.getRatingDate();
		String duration = s.getDuration();
		String payMonth = s.getPayMonth();
		String payDay = s.getPayDay();
		String subproject_payDate = payDate(subproject_interestDate,
				subproject_expiryDate, payMonth, payDay, s.getPaytype());
		String subproject_duration_year = String.valueOf(duration_year(
				subproject_interestDate, subproject_expiryDate));
		String subproject_duration_day = String.valueOf(duration_day(
				subproject_interestDate, subproject_expiryDate));

		map.put("subproject_name", subproject_name);
		map.put("subproject_short_name", subproject_short_name);
		map.put("subproject_code", subproject_code);
		map.put("subproject_collect", subproject_collect);
		map.put("subproject_collect_yiyuan", toYiyuan(subproject_collect));
		map.put("subproject_collect_fen", toFen(subproject_collect));
		map.put("subproject_collect_wanyuan", toWanyuan(subproject_collect));
		map.put("subproject_collect_douhao", toDouhao(subproject_collect));
		map.put("subproject_interestDate", subproject_interestDate);
		map.put("subproject_interestDate_nyr",
				datetoNYR(subproject_interestDate));
		map.put("subproject_expiryDate", subproject_expiryDate);
		map.put("subproject_expiryDate_nyr", datetoNYR(subproject_expiryDate));
		map.put("subproject_rate", subproject_rate);
		map.put("subproject_interestType", subproject_interestType);
		map.put("subproject_payType", subproject_payType);
		map.put("subproject_payNumber", subproject_payNumber);
		map.put("subproject_credit", subproject_credit);
		map.put("subproject_rating", subproject_rating);
		map.put("subproject_memo", subproject_memo);
		map.put("subproject_isTrade", subproject_isTrade);
		map.put("subproject_ratingDate", subproject_ratingDate);
		map.put("subproject_ratingDate_nyr", datetoNYR(subproject_ratingDate));
		map.put("duration", duration);
		map.put("payMonth", payMonth);
		map.put("payDay", payDay);
		map.put("subproject_payDate", subproject_payDate);
		map.put("subproject_duration_day", subproject_duration_day);
		map.put("subproject_duration_year", subproject_duration_year);

		List<Buyer> buyers = s.getBuyers();

		for (Buyer b : buyers) {
			Map<String, Object> map2 = new HashMap<String, Object>();
			String buyer_name = b.getName();
			String buyer_code = b.getCode();
			Integer buyer_number = b.getNumber();
			String buyer_id = b.getBuyerId();
			String buyer_tradeunit = b.getTradeUnit();
			map2.put("sq", index);
			map2.put("subproject_code", subproject_code);
			map2.put("buyer_name", buyer_name);
			map2.put("buyer_code", buyer_code);
			map2.put("buyer_number", buyer_number);
			map2.put("buyer_id", buyer_id);
			map2.put("buyer_tradeunit", buyer_tradeunit);
			map2.put("sysdate", sysdate);
			list2.add(map2);
			index++;
		}

		map.put("buyer", list2);

		return map;
	}

	public static String datetoNYR(Date date) {
		Calendar ca = Calendar.getInstance();
		ca.setTime(date);
		String a = ca.get(Calendar.YEAR) + "年" + (ca.get(Calendar.MONTH) + 1)
				+ "月" + ca.get(Calendar.DAY_OF_MONTH) + "日";
		return a;

	}

	public static String getYear(Date date) {
		Calendar ca = Calendar.getInstance();
		ca.setTime(date);
		String a = String.valueOf(ca.get(Calendar.YEAR));
		return a;
	}

	public static String getMonth(Date date) {
		Calendar ca = Calendar.getInstance();
		ca.setTime(date);
		String a = String.valueOf(ca.get(Calendar.MONTH) + 1);
		return a;
	}

	public static String getDay(Date date) {
		Calendar ca = Calendar.getInstance();
		ca.setTime(date);
		String a = String.valueOf(ca.get(Calendar.DAY_OF_MONTH));
		return a;
	}

	public static String toYiyuan(String c) {
		return String
				.valueOf(Math.round((Float.valueOf(c) / 100000000.0) * 100) / 100.0);
	}

	public static String toWanyuan(String c) {
		return String
				.valueOf(Math.round((Float.valueOf(c) / 10000.0) * 100) / 100.0);
	}

	public static String toShou(String c) {
		return String
				.valueOf(Integer.valueOf(c) / 1000) ;
	}

	public static String toFen(String c) {
		return String
				.valueOf(Integer.valueOf(c) / 100) ;
	}

	public static String toDouhao(String c) {
		c = new StringBuilder(c).reverse().toString();
		String c2 = "";
		for (int i = 0; i < c.length(); i++) {
			if (i * 3 + 3 > c.length()) {
				c2 += c.substring(i * 3, c.length());
				break;
			}
			c2 += c.substring(i * 3, i * 3 + 3) + ",";
		}
		if (c2.endsWith(",")) {
			c2 = c2.substring(0, c2.length() - 1);
		}
		c2 = new StringBuilder(c2).reverse().toString();
		return c2;
	}

	public static String payDate(Date startDate, Date endDate, String month,
			String day, int payType) {
		if (payType != 3 && payType != 4) {
			Calendar ca = Calendar.getInstance();
			ca.setTime(startDate);
			String startMonth = "";
			if (Integer.valueOf(ca.get(Calendar.MONTH) + 1) / 10 != 1) {
				startMonth = "0" + Integer.valueOf(ca.get(Calendar.MONTH) + 1);
			} else
				startMonth = String.valueOf(Integer.valueOf(ca.get(Calendar.MONTH) + 1));
			String startTime = ca.get(Calendar.YEAR) + "-" + startMonth + "-"
					+ ca.get(Calendar.DAY_OF_MONTH);
			int startYear = ca.get(Calendar.YEAR);
			ca.setTime(endDate);
			String endMonth = "";
			if (Integer.valueOf(ca.get(Calendar.MONTH) + 1) / 10 != 1) {
				endMonth = "0" + Integer.valueOf(ca.get(Calendar.MONTH) + 1);
			} else
				endMonth = String.valueOf(Integer.valueOf(ca.get(Calendar.MONTH) + 1));
			String endTime = ca.get(Calendar.YEAR) + "-" + endMonth + "-"
					+ ca.get(Calendar.DAY_OF_MONTH);
			int endYear = ca.get(Calendar.YEAR);
			StringBuffer s = new StringBuffer();
			String m = "";
			String payTime = "";
			for (int index = startYear; index >= startYear && index <= endYear; index++) {
				if (month == null) {
					s.append(" ");
					return s.toString();
				}
				if (day == null) {
					s.append(" ");
					return s.toString();
				}
				for (int i = 0; i < month.split(",").length; i++) {
					m = String.valueOf(month.split(",")[i]);
					if (Integer.valueOf(month.split(",")[i]) / 10 != 1) {
						m = "0" + m;
					}
					payTime = index + "-" + m + "-" + day;
					if (payTime.compareTo(startTime) < 0
							|| payTime.compareTo(endTime) > 0) {
						continue;
					} else
						s.append(index + "年" + m + "月" + day + "日" + "，");

				}
			}
			return s.toString();
		} else {
			Calendar ca = Calendar.getInstance();
			ca.setTime(endDate);

			return ca.get(Calendar.YEAR) + "年" + (ca.get(Calendar.MONTH) + 1)
					+ "月" + ca.get(Calendar.DAY_OF_MONTH) + "日" + "，";
		}

	}

	public static int duration_day(Date startDate, Date endDate) {
		Calendar ca1 = Calendar.getInstance();
		Calendar ca2 = Calendar.getInstance();
		ca1.setTime(startDate);
		ca2.setTime(endDate);
		logger.info("duration_day--startDate("+SnoopyUtil.dateFormat.format(ca1.getTime())+"),millis("+ca1.getTimeInMillis()+")");
		logger.info("duration_day--endDate:"+SnoopyUtil.dateFormat.format(ca2.getTime())+"),millis("+ca2.getTimeInMillis()+")");
		long diff = ca2.getTimeInMillis() - ca1.getTimeInMillis();
		int days = new Long(diff / (1000 * 60 * 60 * 24)).intValue();
		logger.info("duration_day result:diff("+diff+"),days("+days+")");
		return days;
	}

	public static float duration_year(Date startDate, Date endDate) {
		Calendar ca1 = Calendar.getInstance();
		Calendar ca2 = Calendar.getInstance();
		ca1.setTime(startDate);
		ca2.setTime(endDate);
		long diff = ca2.getTimeInMillis() - ca1.getTimeInMillis();
		int days = new Long(diff / (1000 * 60 * 60 * 24)).intValue();
		Float y = Math.round(days / 365.0f * 100) / 100.0f;
		return y;
	}

}
